//
//  LBSaomangAreaVC.m
//  V-Learn
//
//  Created by Apple on 2019/1/1.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "LBSaomangAreaVC.h"
#import "LBSaomangCell.h"

#import "LBHomeCoureseModel.h"
#import "LBSaomiaoAreaModel.h"
#import "LBCoureseDetailVC.h"
#import "LBKeshiDetailVC.h"
#import "LBKeshDetailVC.h"
#import "WKScanerDetailVC.h"
#import "WKSaomangtuwenDetailViewController.h"
@interface LBSaomangAreaVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation LBSaomangAreaVC{
    NSInteger  _page;
    NSInteger  _currentCount;
    NSInteger  _pageIndex;
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LBNAVBAR_HEIGHT, ScreenW, ScreenH-LBNAVBAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    //下拉刷新
    [self downRefreshData];
    
}
- (void)downRefreshData{
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadDownNewDataSecond];
    }];
    
    //2.上拉
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadUpNewDataSecond)];
    //在刷新数据覆盖不显示数据的 cell 的分割线,如果不设置,则会显示 cell 的分割线
    UIView *footView = [UIView new];
    self.tableView.tableFooterView = footView;
    
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark -- 下拉刷新
-(void)loadDownNewDataSecond{
    //默认页数、条数
    _page = 1;
    _currentCount = 10;

    [self requestDataSaomang:_currentCount page:_page];

}
#pragma mark -- 上拉加载
-(void)loadUpNewDataSecond{
    _page++;//页数
    _currentCount = 10;
//    _pageIndex = _page*_currentCount;
    [self requestDataSaomang:_currentCount page:_page];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self setupUI];

}
- (void)setupUI{
    self.navigationItem.title = @"扫盲区";
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = [UIView new];
    
    if (@available(iOS 11.0, *)) {     self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
    }else {
            self.automaticallyAdjustsScrollViewInsets = NO;
    }
}
#pragma mark - 请求数据
- (void)requestDataSaomang:(NSInteger)currentCount page:(NSInteger)page{
    
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    NSString *courrentCountNew = [NSString stringWithFormat:@"%ld",(long)currentCount];
    NSString *pageNew = [NSString stringWithFormat:@"%ld",(long)page];
    [data setObject:pageNew forKey:@"page"];
    [data setObject:courrentCountNew forKey:@"num"];
    [WYHXNetWorkManager get:WKFreeCourseList_WKURL params:data completionHandler:^(id responseObj, NSError *error) {
        NSLog(@"%@",responseObj);
//        NSLog(@"%@",error);
        NSDictionary *dict = (NSDictionary *)responseObj;
        NSString *code = dict[@"code"];
        
        if ([code integerValue]==200) {
            if (page==1&&self.dataArray.count>0) {
                [self.dataArray removeAllObjects];
                [self.tableView reloadData];
            }
            NSArray *arr = [LBSaomiaoAreaModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];
        
            [self.dataArray addObjectsFromArray:arr];
            }
        
            [self.tableView.mj_footer endRefreshing];
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
        
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LBSaomangCell *cell = [LBSaomangCell cellWithTableView:tableView];
    cell.freeIcon.hidden = NO;
    cell.priceLab.hidden = YES;
    LBSaomiaoAreaModel *model = self.dataArray[indexPath.section];
    cell.saomangModel = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LBSaomiaoAreaModel *model = self.dataArray[indexPath.section];
    //0 视频
    //1 音频
    //2 图文
    if ([model.courseType isEqualToString:@"2"]) {
//        LBKeshDetailVC *vc = [LBKeshDetailVC new];
        WKSaomangtuwenDetailViewController *vc = [WKSaomangtuwenDetailViewController new];
        vc.courseId = model.courseId;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        WKScanerDetailVC *vc = [WKScanerDetailVC new];
        vc.courseId = model.courseId;
        vc.titleString = model.courseName;
        vc.detailString = model.coursePresent;
        vc.courseType = model.courseType;
        vc.thunbnaiUrl = [NSString stringWithFormat:@"%@%@",Public_URL,model.courseImage];
        NSLog(@"%@",vc.thunbnaiUrl);
        [self.navigationController pushViewController:vc animated:YES];
    }

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
